# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=keycloak
pkgver=23.0.1
pkgrel=0
pkgdesc="Open source identity and access management for modern applications and services"
url="https://www.keycloak.org"
# armhf,armv7,ppc64le,riscv64,x86: blocked by openjdk17
# s390x: blocked by java-netty-transport-native
arch="noarch !armhf !armv7 !ppc64le !riscv64 !s390x !x86"
license="Apache-2.0"
depends="
	java-jre-headless
	java-netty-transport-native
	"
makedepends="
	bash
	maven
	nodejs
	npm
	java-jdk
	"
pkgusers="keycloak"
pkggroups="keycloak"
install="$pkgname.pre-install"
subpackages="
	$pkgname-openrc
	$pkgname-bash-completion
	$pkgname-zsh-completion
	"
source="https://github.com/keycloak/keycloak/archive/$pkgver/$pkgname-$pkgver.tar.gz
	support-for-readonly-usr-share.patch
	remove-deps.patch
	keycloak.conf
	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotated
	"
# net: fetches dependencies from Maven repositories
options="!check net"

# NOTE: Maven 3 dropped support for the M2_HOME variable.
export MAVEN_ARGS="--batch-mode -Duser.home=$srcdir/.home"

prepare() {
	default_prepare

	# Workaround for frontend-maven-plugin to use system-provided node and npm.
	# https://github.com/eirslett/frontend-maven-plugin/issues/1089
	mkdir -p node
	cp -r /usr/lib/node_modules node/
	ln -s /usr/bin/node node/
	ln -s /usr/bin/npm node/
	ln -s /usr/bin/npx node/
	ln -s ../node js/
}

build() {
	# NOTE: -Dskip.installnodenpm doesn't work, frontend-maven-plugin always
	#  downloads prebuilt node and npm, unless it's already present in the
	#  ./node directory and the version matches.
	local mvn_opts="-DskipTests -Dnode.version=$(node --version)"

	cd quarkus
	mvn -f ../pom.xml install $mvn_opts -DskipExamples -DskipTestsuite
	mvn install $mvn_opts
	tar -C dist -xf dist/target/keycloak-*.tar.gz

	cd dist/keycloak-*
	./bin/kc.sh tools --optimized completion > _kc
}

package() {
	cd quarkus/dist/keycloak-*

	local destdir="$pkgdir/usr/share/$pkgname"
	local builddir="$(pwd)"

	mkdir -p "$destdir"
	cp -r bin lib providers themes version.txt "$destdir"/
	rm -f "$destdir"/bin/*.bat
	rm -f "$destdir"/bin/*.orig

	install -D -m644 _kc "$pkgdir"/usr/share/bash-completion/completions/kc
	install -D -m644 _kc "$pkgdir"/usr/share/zsh/site-functions/_kc

	cd "$pkgdir"

	mkdir -p usr/bin
	local i; for i in kc kcadm kcreg; do
		ln -s ../share/$pkgname/bin/$i.sh usr/bin/$i
	done

	install -d -m750 -o keycloak -g keycloak \
		var/lib/$pkgname \
		var/lib/$pkgname/build \
		var/lib/$pkgname/data \
		var/log/$pkgname
	ln -s /var/lib/$pkgname/data "$destdir"/data

	mv "$destdir"/lib/quarkus "$destdir"/lib/quarkus.dist
	ln -s /var/lib/$pkgname/build/quarkus "$destdir"/lib/quarkus

	install -d -m750 -o root -g keycloak etc/$pkgname
	install -D -m640 -o keycloak -g keycloak -t etc/keycloak/ \
		"$builddir"/conf/cache-ispn.xml \
		"$srcdir"/keycloak.conf
	ln -s /etc/$pkgname "$destdir"/conf

	install -D -m755 "$srcdir"/$pkgname.initd etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd etc/conf.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.logrotated etc/logrotate.d/$pkgname
}

sha512sums="
248d12f14d044d6cadac7a27a3f71a364b77d6c9f823f84e076108f55a901edaf843a004b580fb744ac33b8e53a3dfa39003cd84ae96f387ec313a0755aa869d  keycloak-23.0.1.tar.gz
6f75deab3c4c7a415f4d00335aad5aa8a8d1d5f064b16a2270cf7c7b6c0a52b016c2c3f8ba5fe38702f37e896a170e98eb4332713fa20183d432df9bf4a0294e  support-for-readonly-usr-share.patch
934998230c109a8fbf84b6967c93203f45d0d6d1fdcaf2b7302a57d70143aa1394deb20f898347a21dc2e4df95f189758baf94f8cacd527576ee2d19917ed761  remove-deps.patch
2fc1a25796fa2077a253b72e8f9900ab33cb4ab652f8c1ecc8450c79a6efb6398a6000adc57a45148abd9497a766f811cf7a12997956fbb3e1c76b8e6f2bdb7e  keycloak.conf
24682863dfbb652339712a0ed781bb619dc747db283b3916c49f8b5a2dc6497cd4489e66b5f90dc98a1e37d1a186bf08bd176d8e16a3f56beeaa68aac4ef15d6  keycloak.initd
1a763c430ab1b614ee96f0c80634143baf5ae00b5b7d76096ad3230792982ffafc91148be90dc3b5c8672cb05df8d636f5003445a7138b5fe97428113a08b3b3  keycloak.confd
5fe5972a71c630e7335f6a73b10d7258dcbb0448f3a9dab2aac94fe9a4905c0cbb5ebbc4c74113696296e19a207fd019c72430ff53feab7255edce91ed89fd11  keycloak.logrotated
"
